#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
 
void Worker()
{
    int cnt =4;
    while (cnt)
    {
        printf("I am child process, pid: %d, ppid: %d, cnt: %d\n", getpid(), getppid(), cnt--);
        sleep(1);
    }
    exit(0);
}
 
int main()
{
    pid_t id = fork();
    if (id == 0)
    {
        // child
        Worker();
    }
    else
    {

        // father
        while(1)
        {

             pid_t rid = waitpid(id,NULL,WNOHANG);
             if (rid > 0)
             {
             	while(1)
             	{
                	printf("wait success\n");
                	sleep(1);
                }
             }
             else if(rid == -1)
             {
             	while(1)
             	{
             		printf("wait fail\n");
                	sleep(1);
                }
             }
             else
             {
               	printf("haha\n");
               	sleep(1);
          	 }
        }
        
    }
    return 0;
}
